package com.example.my_model;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.example.common.filed.EventBusAction;
import com.example.common.mvvm.view.BaseFragment;
import com.example.common.providerservice.CheckLoginAPI;
import com.example.imageload_sdk.ImageLoad;
import com.example.my_model.accountentity.DataDTO;
import com.example.my_model.databinding.FragmentUserBinding;
import com.example.my_model.room.UserBean;
import com.example.my_model.room.UserEntity;
import com.tencent.tauth.Tencent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserFragment extends BaseFragment<FragmentUserBinding,UserViewModel> {

    private CheckLoginAPI checkLoginAPI;
    private DataDTO userData;
    private CircleImageView headportrait;
    private TextView userName;


    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBind() {
        return FragmentUserBinding.inflate(inflater);
    }

    @Override
    public void initView() {
       checkLoginAPI = (CheckLoginAPI) ARouter.getInstance().build("/user/checklogin").navigation();
        if (!checkLoginAPI.checkLogin())
            ARouter.getInstance().build("/user/LoginActivity").navigation();
        headportrait = v.headportrait;
        userName = v.userName;
    }

    @Override
    public void initData() {
        if (checkLoginAPI.checkLogin())
            vm.getUser();

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void receiveData(Map<String, Object> data) {
        userData = (DataDTO) data.get("VIDEODATA");
        ImageLoad.loadMatchImg(userData.avatar, headportrait);
        userName.setText(userData.name);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventAction(EventBusAction eventBusAction){
        if (eventBusAction.action.equals("refreshUserInfo")){
            vm.getUser();
        }
    }
}
